<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\library\services\FangAttrsService;
use App\library\services\SecondHouseService;
use Illuminate\Http\Request;
use App\Models\SecondHandHouse as SecondModel;
use Illuminate\Support\Facades\Redis;

class SecondHandHouseController extends Controller
{
    //二手房首页列表
    /**
     * author:wiiliam
     * @return mixed
     */
    public function secondIndexList()
    {
        $info = SecondModel::select('image', 'unit_price','price','house_estate')->paginate(3);
        $info && $info = $info ->toArray();
        return $this->success('成功',$info);
    }

    /**
     * Author:YangXinPeng
     * 根据售卖方式获取二手房列表
     * @param Request $request
     * @return array
     */
    public function secondBySaleType(Request $request)
    {
        $saleType = $request->get('sale_type');
        $province = $request->get('province');
        $data = SecondHouseService::getDataBySale($saleType,$province);
        return $data;
    }

    /**
     * Author:YangXinPeng
     * 根据ID获取二手房信息
     * @param Request $request
     * @return array
     */
    public function secondById(Request $request)
    {
        $id = $request->get('id');
        $data = SecondHouseService::getDataById($id);
        return $data;
    }

    /**
     * 定期二手房源同步redis
     */
    public function toRedis()
    {
        SecondHouseService::dbToRedis();
    }

    /**
     * Author:YangXinPeng
     * GEO查询范围内二手房源信息
     * @param Request $request
     * @return mixed
     */
    public function findHouse(Request $request)
    {
        $latitude = $request->get('latitude');
        $longitude = $request->get('longitude');
        $province = $request->get('province');
        $number  =$request->get('number',5);
        $key = config('setting.map').$province;
        if (!(Redis::exists($key))){
            $this->toRedis();
        }
        $res = SecondHouseService::findByRedis($province,$latitude,$longitude,$number);
        return $this->res($res);
    }


    /**
     * Author:YangXinPeng
     * 根据经纬度条件查询二手房源
     * @param Request $request
     * @return mixed
     */
    public function findByMap(Request $request)
    {
        $latitude = $request->get('latitude');
        $longitude = $request->get('longitude');
        $res = SecondHouseService::findByItude($latitude,$longitude);
        return $this->res($res);
    }

    /**
     * Author:YangXinPeng
     * 售价条件
     * @return mixed
     */
    public function getPrices()
    {
        $data = FangAttrsService::fangPrice();
        return $this->res($data);
    }

    /**
     * Author:YangXinPeng
     * 房型条件
     * @return mixed
     */
    public function getFangType()
    {
        $data = FangAttrsService::getHouseType();
        return $this->success('成功',$data);
    }

    /**
     * Author:YangXinPeng
     * 朝向条件
     * @return mixed
     */
    public function getFangForward()
    {
        $data = FangAttrsService::getFangDirection();
        return $this->success('成功',$data);
    }



    /**
     * Author:YangXinPeng
     * 装修条件
     * @return mixed
     */
    public function getFangFit()
    {
        $data = FangAttrsService::getFangFit();
        return $this->success('成功',$data);
    }


    /**
     * Author:YangXinPeng
     * @param Request $request
     * 查询经纪人名下的所有房源
     * @return mixed
     */
    public function getFangByAgent(Request $request)
    {
        $aid = $request->get('agent');
        $res = SecondHouseService::findByAid($aid);
        return $this->res($res);
    }

    /**
     * Author:YangXinPeng
     * 复合条件查询房源
     * @param Request $request
     * @return mixed
     */
    public function getFang(Request $request)
    {
        $params = $request->all();
        $aid = $request->uid;
        $arr = json_decode($params['data'],true);
        $where = [];
        foreach ($arr as $key=>$value){
           foreach ($value as $k=>$v){
                  $where[$k] = $v;
           }
        }
        $res = SecondHouseService::findFang($where,$aid);
        return $this->res($res);
    }



}
